﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Console
{
    public static class AppInfoRegistryUtil
    {
        /*
         *
        -当前版本信息
        Version:string
        -程序所在目录
        Directory:string
        -程序执行文件路径
        App:string
        -应用程序的名称
        Name:string
        -图标信息,用于显示在更新程序上
        Icon:string
        -信息介绍页面
        InfomationUrl:string
        */
        public const string Version = "Version";
        public const string Directory = "Directory";
        public const string App = "App";
        public const string Name = "Name";
        public const string Icon = "Icon";
        public const string InfomationUrl = "InfomationUrl";
        public const string Company = "Company";
        public const string Product = "Product";

        public const string RelativeProductPath = @"{0}\{1}\{2}\";

        /// <summary>
        /// 从注册表信息中读取所有已安装的项目信息
        /// <para>KEY_LOCAL_MACHINE\SOFTWARE\[Company]\[PRODUCTS]\</para>
        /// </summary>
        /// <param name="company"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        public static List<AppInfo> GetAllAppInfo(string company, string product)
        {
            return null;
        }

        /// <summary>
        /// 往注册表中存放应用程序信息
        /// <para>KEY_LOCAL_MACHINE\SOFTWARE\[Company]\[PRODUCTS]\[appInfo.Type]\</para>
        /// </summary>
        /// <param name="company"></param>
        /// <param name="product"></param>
        /// <param name="appInfo"></param>
        /// <returns></returns>
        public static bool SaveAppInfo(string company, string product, AppInfo appInfo)
        {
            string upperAppType = appInfo.Type.ToUpper();

            try
            {
                RegistryUtility.WriteString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Company, appInfo.Company);
                RegistryUtility.WriteString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Product, appInfo.Product);
                RegistryUtility.WriteString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Name, appInfo.Name);
                RegistryUtility.WriteString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Directory, appInfo.Directory);
                RegistryUtility.WriteString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.App, appInfo.AppFile);
                RegistryUtility.WriteString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Icon, appInfo.IconFile);
                RegistryUtility.WriteString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Version, appInfo.Version.ToString(4));
                return true;
            }
            catch(Exception ex)
            {
                //Common.Logger.ErrorToTag("Registry", "保存应用程序:{0},type:{1}信息失败,ex:{2}",
                //    appInfo.Name,
                //    appInfo.Type,
                //    ex);
                return false;
            }
        }

        /// <summary>
        /// 获取注册表中应用程序的信息
        /// <para>KEY_LOCAL_MACHINE\SOFTWARE\[Company]\[PRODUCTS]\[appType]\</para>
        /// </summary>
        /// <param name="company"></param>
        /// <param name="product"></param>
        /// <param name="appType"></param>
        /// <returns></returns>
        public static AppInfo GetAppInfo(string company, string product, string appType)
        {
            string upperAppType = appType.ToUpper();
            if (RegistryUtility.IsPathExist(Wrapper(company, product, upperAppType)))
            {
                AppInfo app = new AppInfo();
                app.Company = RegistryUtility.ReadString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Company);
                app.Product = RegistryUtility.ReadString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Product);
                app.Name = RegistryUtility.ReadString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Name);
                app.Directory = RegistryUtility.ReadString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Directory);
                app.AppFile = RegistryUtility.ReadString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.App);
                app.IconFile = RegistryUtility.ReadString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Icon);
                app.Version = new Version(RegistryUtility.ReadString(Wrapper(company, product, upperAppType), AppInfoRegistryUtil.Version));
                app.Type = upperAppType;
                return app;
            }
            return null;
        }

        private static string Wrapper(string company, string product, string path)
        {
            return string.Format(RelativeProductPath, company, product, path);
        }
    }
}
